package com.vcommon.core.utils
{

/**
 * The ArrayUtil utility class is an all-static class with methods for working with Array objects.
 * @author Sean Zou
 *
 */
public class ArrayUtil
{
	/**
	 * Switch a objcet to array.
	 * @param obj
	 * @return
	 *
	 */
	public static function toArray(obj:Object):Array
	{
		if (!obj)
		{
			return [];
		}
		else if (obj is Array)
		{
			return obj as Array;
		}
		else if (obj is XMLList)
		{
			var result:Array = new Array();
			var len:uint = XMLList(obj).length();
			for (var i:uint = 0; i < len; i++)
			{
				result.push(XMLList(obj)[i]);
			}
			return result;
		}
		else
		{
			return [ obj ];
		}
	}
	
	/**
	 * Remove all instances of the specified value from the array,
	 * @param arr The array from which the value will be removed
	 * @param value The object that will be removed from the array.
	 */
	public static function removeValueFromArray(arr:Array, value:Object):void
	{
		var len:uint = arr.length;
		
		for (var i:Number = len; i > -1; i--)
		{
			if (arr[i] === value)
			{
				arr.splice(i, 1);
			}
		}
	}
	
	/**
	 * 将一个数组附加在另一个数组之后
	 * 
	 * @param arr   目标数组
	 * @param value 附加的数组
	 * 
	 */
	public static function append(arr:Array, value:Array):void
	{
		arr.push.apply(null,value);
	}
}
}
